<?php
namespace app\console\controller;
use app\model\Device as SelfModel;
use app\model\DeviceRecords;
use app\model\DeviceSigning;
use app\model\Project;
use app\model\LeasingCompany;

use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\facade\Request;
use think\facade\Route;
use think\facade\Session;
use think\facade\View;
use think\facade\Db;
use think\facade\Cache;
use think\facade\Cookie;
class Device extends Base
{



    public function initialize()
    {
        parent::initialize();
        $this->model = "\app\model\Device";
        View::assign('model', $this->model);
        $this->list_rows = 20;
        $this->where = [['company_id','in',Session::get('data_id_str')]];
        if(!empty(input('company_id'))){
            $this->where = [['company_id','in',input('company_id')]];
        }
        
    }




    public function select()
    {
        $con = 'BalanceFault01';
        $device_name = 'ZPH0001';
        $status = [
            'Openn'=>'开机',
            'Close'=>'关机',
            'Pause'=>'暂停',
            'BalON'=>'平衡(开)',
            'BaOFF'=>'平衡(关)',
            'OvlON'=>'超载(开)',
            'OvOFF'=>'超载(关)',
            'BalanceFault01'=>'平衡故障',
            'LimitFault02'=>'限位故障',
            'OverloadFault03'=>'超载故障',
            'MotorFault04' =>'电机故障',
            'HangUpFault05' => '挂断故障'
        ];
//        if (in_array($con, ['BalanceFault01'])) {
//            $content = $status[$con];
//            $is_insert = true;
//            $device = \app\model\Device::where('title',$device_name)->select();
//            dump($device);
//            if(!$device->isEmpty()) {
//                foreach ($device as $v) {
//                    if ($v['BalanceFault01'] == 1) {
//                        $v->save(['error' => $content]);
//                    }
//                }
//            }
//            ECHO 1234;
//        }


//        $data = \think\facade\Db::connect('mongodb')
//            ->table('one')
//            ->insert(['name'=>11,'age'=>2]);
//       $d = DeviceRecords::table('one')->insert(['name'=>11,'age'=>2,'create_time'=>date('Y-m-d H:i:s')]);
//
//       $data =  \think\facade\Db::connect('mongodb')->table('one')->where('name',111)->order('_id','desc')->find();

       //halt($data);

        $project = Project::order([ 'id' => 'desc'])->where($this->where)->select()->toArray();
        View::assign('project', $project);
        $where = [['id','in',Session::get('data_id_str')]];

         $leasing_company = LeasingCompany::order([ 'id' => 'desc'])->where($where)->select()->toArray();
         View::assign('leasing_company', $leasing_company);
//            $balance = \app\console\model\Config::where('id',11)

    }

    public function active()
    {


        Db::startTrans();
        try {
            $param = $this->request->param();

            $year_free = \app\console\model\Config::where('id', 110)->value('varvalue');
            $balance = LeasingCompany::where('id', $param['company_id'])->value('balance');
            if ($balance < $year_free) {
                return json(['code' => 400, 'msg' => '公司账户余额不足,请充值']);
            }
            $deivce = \app\model\Device::where('id', $param['id'])->find();
            if ($deivce['start_time'] == '0000-00-00 00:00:00') {
                \app\model\Device::where('id', $param['id'])->update(['start_time' => date('Y-m-d H:i:s')]);
                $start_time = date('Y-m-d H:i:s');
            }else{
                $start_time = $deivce['end_time'];
            }
            if ($deivce['end_time'] == '0000-00-00 00:00:00') {
               // $end_time = time() + 3600 * 24 * 365;
                $end_time = strtotime('+1 year', time());
            } else {
                //$end_time = strtotime($deivce['end_time']) + 3600 * 24 * 365;
                $end_time = strtotime('+1 year', strtotime($deivce['end_time']));
            }
            $year_free_time = DB::name('config')->where('id',111)->value('varvalue');
            $delay_time = strtotime('+'.$year_free_time. 'day',$end_time);
            $ret = \app\model\Device::where('id', $param['id'])->update(['end_time' => date('Y-m-d H:i:s', $end_time),'delay_time'=>date('Y-m-d H:i:s',$delay_time),'use_status'=>'已激活']);
            LeasingCompany::where('id', $param['company_id'])->dec('balance', $year_free)->update();
            $new_balance = LeasingCompany::where('id', $param['company_id'])->value('balance');
            $company_id = LeasingCompany::where('id', $param['company_id'])->value('id');
            $project_title = Project::where('id',$param['project_id'])->value('title');
            if(empty($project_title)){
                return json(['code' => 400, 'msg' => '请设置项目']);
            }


            $data = ['id_no'=>$deivce['id_no'],'name_no'=>$deivce['name_no'],'company_id'=> $company_id ,'device_id' => $param['id'],'project_id'=> $param['project_id'],'project_title'=> $project_title, 'title' => $param['title'], 'start_time'=> $start_time,'end_time'=>date('Y-m-d H:i:s', $end_time),'delay_time'=>date('Y-m-d H:i:s',$end_time+3600*24*$year_free_time), 'price' => $year_free, 'balance' => $new_balance];

            DeviceSigning::create($data);
            Db::commit();
            return json(['code' => 200, 'msg' => '签约成功']);
        } catch (\Exception $e) {
            Db::rollback();
            return json(['code' => 500, 'msg' => $e->getMessage()]);
        }

    }







}